var app = getApp()
var tempFilePaths;
var avatar=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hideUpload: true,
    uploadImg: [],
    imgNum: 0,
    name: '',
    phone: '',
    address: '',
    do_well_in: '',
    worker_count: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = app.globalData.factoryInfo
    let tel = app.globalData.tel
    this.setData(data)
    this.setData({
      phone:tel
    })
    console.log(this.data)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  upload: function () {
    var that = this
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res.tempFiles)
        var num = that.data.imgNum
        var img = that.data.uploadImg
        tempFilePaths = res.tempFilePaths
        that.setData({
          uploadImg: img.concat(res.tempFilePaths)
        })
        wx.uploadFile({
          url: 'https://qili.docdic.com/api/file', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          success:function(res){
            console.log(res)
            avatar.push(res.data)
            console.log(avatar)
          }
        })
      }
    })
  },
  del_img: function (e) {
    let index = e.currentTarget.dataset.index
    let imgUrl = this.data.uploadImg
    imgUrl.splice(index, 1)
    console.log(index)
    console.log(imgUrl)
    this.setData({
      uploadImg: imgUrl
    })
  },
  formSubmit: function (e) {
    console.log(e)
    var that = this
    let name = e.detail.value.name
    let tel = e.detail.value.tel
    let adress = e.detail.value.adress
    let worksNum = e.detail.value.worksNum
    let serverInfo = e.detail.value.serverInfo
    let factoryImg = this.data.uploadImg
    let url = ''
    if (name == '') {
      wx.showToast({
        title: '请输入工厂名字',
        icon: 'success',
        duration: 2000
      })
      return false
    }
    if (!(/^1[34578]\d{9}$/.test(tel))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (adress == '') {
      wx.showToast({
        title: '请输入工厂地址',
        icon: 'success',
        duration: 2000
      })
      return false
    }
    if (worksNum == '') {
      wx.showToast({
        title: '请输入工厂人数',
        icon: 'success',
        duration: 2000
      })
      return false
    }
    if (serverInfo == '') {
      wx.showToast({
        title: '请输入服务范畴',
        icon: 'success',
        duration: 2000
      })
      return false
    }
    if (factoryImg.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'success',
        duration: 2000
      })
      return false
    }
    console.log(avatar)
    let params = {
      name: name,
      address: adress,
      phone: tel,
      worker_count: worksNum,
      do_well_in: serverInfo,
      images: avatar
    }
    let token = wx.getStorageSync('token')
    let userId = wx.getStorageSync('userId')
    let has_factory = wx.getStorageSync('has_factory')

    if (Number(has_factory) === 1) {
      url = 'https://qili.docdic.com/api/factory/' + that.data.factory_id+'?token=' + token
    } else {
      url = 'https://qili.docdic.com/api/factory?token=' + token
    }
    wx.request({
      url: url,
      method: 'post',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (Number(res.data.status === 201)) {
          wx.showToast({
            title: '上传成功',
            icon: 'success',
            duration: 1000
          })
          setTimeout(() => {
            wx.switchTab({
              url: '../person',
            })
          }, 600)
        }
      },
      fail: function (err) {
        console.log(err)
        wx.showToast({
          title: '上传失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})